package com.xh3140.android.widget.dialog.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.view.Gravity;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.content.res.ResourcesCompat;

import com.xh3140.android.widget.dialog.params.LoadingParams;
import com.xh3140.android.widget.dialog.utils.LayoutParamsBuilder;
import com.xh3140.android.widget.dialog.utils.ViewUtils;

public class LoadingView extends LinearLayoutCompat {
    private ProgressBar mProgressBar;
    private final AppCompatTextView mSubtitleView;

    public LoadingView(@NonNull Context context) {
        this(context, new LoadingParams(context));
    }

    public LoadingView(@NonNull Context context, @NonNull LoadingParams params) {
        super(context);
        setOrientation(VERTICAL);
        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f));
        mSubtitleView = new AppCompatTextView(context);
        configView(params);
    }

    /**
     * 获取进度条视图
     */
    public ProgressBar getProgressBar() {
        return mProgressBar;
    }

    /**
     * 获取文本框视图
     */
    public AppCompatTextView getSubtitleView() {
        return mSubtitleView;
    }

    /**
     * 配置视图
     *
     * @param params 加载主体视图配置属性
     */
    public void configView(@NonNull LoadingParams params) {
        removeAllViews();
        configProgressBar(params);
        configTextView(params);
    }

    /**
     * 配置进度条视图
     *
     * @param params 加载主体视图配置属性
     */
    private void configProgressBar(@NonNull LoadingParams params) {
        final int defStyleAttr = params.isSpinner ? android.R.attr.progressBarStyle : android.R.attr.progressBarStyleHorizontal;
        mProgressBar = new ProgressBar(getContext(), null, defStyleAttr);
        if (params.drawable == 0) {
            if (params.tintColor != 0) {
                mProgressBar.setIndeterminateTintList(ColorStateList.valueOf(params.tintColor));
            }
        } else {
            mProgressBar.setProgressDrawableTiled(ResourcesCompat.getDrawable(getResources(), params.drawable, null));
        }
        final int width = params.isSpinner ? LayoutParams.WRAP_CONTENT : LayoutParams.MATCH_PARENT;
        setLayoutParams(new LayoutParamsBuilder(LinearLayoutCompat.LayoutParams.class, width, LayoutParams.WRAP_CONTENT)
                .setGravity(Gravity.CENTER_HORIZONTAL)
                .setMarginsRelative(params.margins)
                .build());
        mProgressBar.setIndeterminate(params.isInfinite);
        addView(mProgressBar);
    }

    /**
     * 配置文本框视图
     *
     * @param params 加载主体视图配置属性
     */
    private void configTextView(@NonNull LoadingParams params) {
        mSubtitleView.setText(params.text);
        mSubtitleView.setTextSize(params.textSize);
        mSubtitleView.setTextColor(params.textColor);
        mSubtitleView.setTypeface(mSubtitleView.getTypeface(), params.typeface);
        mSubtitleView.setGravity(params.gravity);
        mSubtitleView.setBackgroundColor(params.backgroundColor);
        if (params.padding != null) {
            // TODO
            final int[] padding = params.padding;
            if (params.isTopSubtitle) {
                setPaddingRelative(padding[0], padding[3], padding[2], padding[1]);
            } else {
                setPaddingRelative(padding[0], padding[1], padding[2], padding[3]);
            }
        }
        mSubtitleView.setLayoutParams(LayoutParamsBuilder.ofWW(LinearLayoutCompat.LayoutParams.class)
                .setGravity(Gravity.CENTER_HORIZONTAL)
                .build());
        ViewUtils.setTextViewHeight(mSubtitleView, params.height);
        mSubtitleView.setVisibility(params.text == null ? GONE : VISIBLE);
        addView(mSubtitleView, params.isTopSubtitle ? 0 : -1);
    }
}
